﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;

namespace Elf.Common
{
    /// <summary>
    /// Simple wrapper for unity resolution.
    /// </summary>
    public class DependencyFactory
    {
        #region Singleton
        /// <summary>
        /// Prevents a default instance of the <see cref="DependencyFactory"/> class from being created.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "It's a Singleton")]
        DependencyFactory()
        {
            Container = new WindsorContainer();
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static DependencyFactory Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Singleton implementation")]
        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Singleton implementation")]
            static Nested()
            {
            }

            internal static readonly DependencyFactory instance = new DependencyFactory();
        }
        #endregion

        /// <summary>
        /// Gets or sets the container.
        /// </summary>
        /// <value>
        /// The container.
        /// </value>
        public IWindsorContainer Container { get; set; }

        /// <summary>
        /// Resolves the type parameter T to an instance of the appropriate type.
        /// </summary>
        /// <typeparam name="T">Type of object to return</typeparam>
        public static T Resolve<T>()
        {
            var ret = default(T);
                ret = Instance.Container.Resolve<T>();
            return ret;
        }
    }
}
